//! @Author: DengLibin
//! @Date: Create in 2023-11-07 10:46:54
//! @Description: user dao

use std::sync::Arc;


use mysql_async::{Row, Value};

use crate::{
    entity::User,
    global::{self, AppState, GlobalResult},
};

use rust_common::mysql::row_value2_string;


/// @Author: DengLibin
/// @Date: Create in 2023-11-07 12:17:13
/// @Description: 查询用户列表
pub async fn list(app_state: Arc<AppState>) -> GlobalResult<Vec<User>> {
    let client = &app_state.mysql_client;
    let sql = "SELECT id, create_by, update_by, username, password, create_time, update_time from user where deleted=false";
    let r = client.query(
        sql,
        vec![],
        |row: Row| {
            let mut u = User{
                id: row.get(0),
                create_by:row.get(1),
                update_by: row.get(2),
                username: row.get(3),
                password: row.get(4),
                create_time: None,
                update_time: None,
                deleted: Some(false)
            };
            let create_time: Option<Value> = row.get(5);
            let update_time: Option<Value> = row.get(6);
            //类型转换
            if let Some(v) = create_time {
                u.create_time = row_value2_string(v);
            }
            if let Some(v) = update_time {
                u.update_time = row_value2_string(v);
            }
            return u;
        }
    ).await;

    global::check_result(r)
}

